// 列车轨迹渲染与实时更新
import * as Cesium from 'cesium'

export function addTrainEntity(viewer, positions, modelUrl) {
  // positions: [{lng, lat, height, time}]
  const property = new Cesium.SampledPositionProperty()
  positions.forEach((pos) => {
    const time = Cesium.JulianDate.fromDate(new Date(pos.time))
    const cartesian = Cesium.Cartesian3.fromDegrees(
      pos.lng,
      pos.lat,
      pos.height || 0
    )
    property.addSample(time, cartesian)
  })
  const entity = viewer.entities.add({
    name: '列车',
    availability: new Cesium.TimeIntervalCollection([
      new Cesium.TimeInterval({
        start: property._property._times[0],
        stop: property._property._times[property._property._times.length - 1],
      }),
    ]),
    position: property,
    orientation: new Cesium.VelocityOrientationProperty(property),
    model: {
      uri: modelUrl,
      scale: 0.5,
    },
    path: {
      show: true,
      leadTime: 0,
      trailTime: 60,
      width: 4,
      material: Cesium.Color.YELLOW,
    },
  })
  return entity
}
